/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui.wizards;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;

import org.isqlviewer.util.LoggableObject;

/**
 * Basic implementation of the Step interface for facilitating wizard development.
 * <p>
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public abstract class AbstractWizardStep extends LoggableObject implements Step {

    public static List<Step> initializeSteps(Collection<Class< ? extends AbstractWizardStep>> steps,
            WizardContext context) {

        ArrayList<Step> newSteps = new ArrayList<Step>();
        Iterator<Class< ? extends AbstractWizardStep>> listIter = steps.iterator();
        AbstractWizardStep previous = null;
        while (listIter.hasNext()) {
            Class< ? extends AbstractWizardStep> clazz = listIter.next();
            AbstractWizardStep step;
            try {
                step = clazz.newInstance();
                step.init(context);
                newSteps.add(step);
                if (previous != null) {
                    step.setPrevious(previous);
                    previous.setNextStep(step);
                }
                previous = step;
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return newSteps;
    }

    private String title = null;
    private String comment = null;
    private Icon image = null;
    private JComponent view = null;
    private Step next = null;
    private Step previous = null;
    private WizardContext activeContext = null;

    public void init(WizardContext context) {

        this.activeContext = context;
    }

    public void activate(WizardContext context) {

    }

    public Step getPrevious() {

        return previous;
    }

    public void setPrevious(Step previous) {

        this.previous = previous;
    }

    public Step getNext() {

        return next;
    }

    public String getComment() {

        return comment;
    }

    public void setComment(String comment) {

        this.comment = comment;
    }

    public Icon getImage() {

        return image;
    }

    public void setImage(Icon image) {

        this.image = image;
    }

    public String getTitle() {

        return title;
    }

    public void setTitle(String title) {

        this.title = title;
    }

    public JComponent getView() {

        return view == null ? new JPanel() : view;
    }

    public void setView(JComponent view) {

        this.view = view;
    }

    public void setNextStep(Step nextStep) {

        this.next = nextStep;
    }

    protected WizardContext getContext() {

        return activeContext;
    }

}
